package TestEqualsHashCode;

import lombok.*;

import java.util.HashSet;
import java.util.Objects;

/**
 * @Author Mr.Wen
 * @Version 1.0
 */
public class Test1 {
    public static void main(String[] args) {
        /*equals判断为true的两个对象,hashcode一定相等*/
        /*不同对象的 hashCode 可能相同；但 hashCode 不同的对象一定不相等*/
        HashSet<Person> people = new HashSet<>();
        people.add(new Person("jack", 18));
        people.add(new Person("jack", 18));
        people.forEach(System.out::println);
    }
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
class Person {
    String name;
    int age;



    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    /*@Override
    public int hashCode() {
        return Objects.hash(name, age);
    }*/

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}